/*
	tux type - tbutton.cpp
 	Copyright (c) 2003 by Linux_Lyb <linux_lyb@sohu.com>
 ____________________________________________________________________________	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2, or (at your option)
	any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

	See the file COPYING in the doc/ directory for more information.
*/

#include <string.h>
#include "tbutton.h"

tButton::tButton()
{
	x=0;
	y=0;
	
}

tButton::~tButton()
{
//free all
	//destroy widget
	gtk_widget_destroy (GTK_WIDGET(image));
	gtk_widget_destroy (GTK_WIDGET(event));
	//free pixbuf
	gdk_pixbuf_unref (pix_n);
	gdk_pixbuf_unref (pix_p);
	gdk_pixbuf_unref (pix_e);
}

gboolean tButton::init(const char *filename,GtkWidget *fixed)
{
gchar *file,*file_p,*file_e;
GdkPixbuf *tmp_pixbuf;

	mousein = FALSE;

	file = new gchar [strlen(filename)+1];
	if (!file) {
		g_print ("Allocation file[%d] Error!! %s \n",strlen(filename),filename);
		return FALSE;
	} else {
		strcpy (file,filename);
	}
	
	// file_p = filename + "_p.png"
	file_p = new gchar [strlen(filename)+7];
	if (!file) {
		g_print ("Allocation file_p[%d] Error!! %s \n",strlen(filename),filename);
		return FALSE;
	} else {
		strncpy (file_p,filename,strlen(filename)-4);
		file_p [strlen(filename)-4]='\0';
		strcat (file_p,"_p.png");		
	}

	// file_e = filename + "_e.png"
	file_e = new gchar [strlen(filename)+7];
	if (!file) {
		g_print ("Allocation file_e[%d] Error!! %s \n",strlen(filename),filename);
		return FALSE;
	} else {
		strncpy (file_e,filename,strlen(filename)-4);
		file_e [strlen(filename)-4]='\0';
		strcat (file_e,"_e.png");
	}

	// Load all image file here
	pix_n = gdk_pixbuf_new_from_file (file,NULL);
	pix_p = gdk_pixbuf_new_from_file (file_p,NULL);
	pix_e = gdk_pixbuf_new_from_file (file_e,NULL);

	event = gtk_event_box_new ();
	image = gtk_image_new_from_pixbuf (pix_n);
	
	gtk_container_add (GTK_CONTAINER(event),image);

	g_signal_connect_swapped (G_OBJECT(event),"button_press_event",
						G_CALLBACK(press_tbutton),this);

	g_signal_connect_swapped (G_OBJECT(event),"enter_notify_event",
						G_CALLBACK(enter_tbutton),this);

	g_signal_connect_swapped (G_OBJECT(event),"leave_notify_event",
						G_CALLBACK(leave_tbutton),this);
	
	gtk_fixed_put (GTK_FIXED(fixed),event,x,y);
	
	gtk_widget_show (image);
	gtk_widget_show (event);

	delete file;
	delete file_p;
	delete file_e;
}

gboolean tButton::create_tbutton(GtkWidget *fixed,const char *filename,void (*runFunc)(gpointer),gpointer funcData)
{
	init(filename,fixed);
	
	func = runFunc;
	data = funcData;
	
	g_signal_connect_swapped (G_OBJECT(event),"button_release_event",
					G_CALLBACK(release_tbutton),this);

	return TRUE;
}

void tButton::do_clicked()
{
	(*(func)) (data);
}

void tButton::change_image(const char *filenames)
{
	gtk_image_set_from_file (GTK_IMAGE(image),filenames);
}

gboolean tButton::release_tbutton(tButton *tbutton)
{
	if ((tbutton -> get_mousein()) == TRUE)
		tbutton -> do_clicked();
	return TRUE;
}

gboolean tButton::press_tbutton(tButton *tbutton)
{
	//draw the button press image
	gtk_image_set_from_pixbuf (GTK_IMAGE(tbutton->image),tbutton -> pix_p);
	return TRUE;
}

gboolean tButton::enter_tbutton(tButton *tbutton)
{
	//draw the button enter image
	if (tbutton -> get_mousein() == FALSE)  {
		gtk_image_set_from_pixbuf (GTK_IMAGE(tbutton->image),tbutton -> pix_e);
		tbutton -> set_mousein(TRUE);
	}
	return FALSE;
}

gboolean tButton::leave_tbutton(tButton *tbutton)
{
	//draw the button leave image
	gtk_image_set_from_pixbuf (GTK_IMAGE(tbutton->image),tbutton -> pix_n);
	tbutton -> set_mousein (FALSE);
	return FALSE;
}
